package com.ruoyi.outcome.controller;

import java.util.Date;
import java.util.List;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.utils.bean.BeanUtils;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.outcome.domain.ResearchPaper;
import com.ruoyi.outcome.domain.vo.ResearchPaperTrack;
import com.ruoyi.outcome.domain.vo.ResearchPatentTrack;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.outcome.domain.ResearchPatent;
import com.ruoyi.outcome.service.IResearchPatentService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 专利管理Controller
 * 
 * @author zxh
 * @date 2025-01-06
 */
@RestController
@RequestMapping("/patent")
public class ResearchPatentController extends BaseController
{
    @Autowired
    private IResearchPatentService researchPatentService;

    /**
     * 查询专利管理列表
     */
    @RequiresPermissions("outcome:patent:list")
    @GetMapping("/list")
    public TableDataInfo list(ResearchPatent researchPatent)
    {
        startPage();
        researchPatent.setIsDeleted(0);
        List<ResearchPatent> list = researchPatentService.selectResearchPatentList(researchPatent);
        return getDataTable(list);
    }

    /**
     * 导出专利管理列表
     */
    @RequiresPermissions("outcome:patent:export")
    @Log(title = "专利管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ResearchPatent researchPatent)
    {
        List<ResearchPatent> list = researchPatentService.selectResearchPatentList(researchPatent);
        ExcelUtil<ResearchPatent> util = new ExcelUtil<ResearchPatent>(ResearchPatent.class);
        util.exportExcel(response, list, "专利管理数据");
    }

    /**
     * 获取专利管理详细信息
     */
    @RequiresPermissions("outcome:patent:query")
    @GetMapping(value = "/{patentId}")
    public AjaxResult getInfo(@PathVariable("patentId") Long patentId)
    {

        return success(researchPatentService.selectResearchPatentByPatentId(patentId));
    }

    /**
     * 新增专利管理
     */
    @RequiresPermissions("outcome:patent:add")
    @Log(title = "专利管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ResearchPatent researchPatent)
    {
        researchPatent.setCreatedBy(SecurityUtils.getUsername());

        ResearchPatentTrack patentTrack= new ResearchPatentTrack();
        BeanUtils.copyProperties(researchPatent, patentTrack);
        patentTrack.setActionType("ADD");
        researchPatentService.insertResearchPatentAudit(patentTrack);
        return toAjax(researchPatentService.insertResearchPatent(researchPatent));
    }

    /**
     * 修改专利管理
     */
    @RequiresPermissions("outcome:patent:edit")
    @Log(title = "专利管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ResearchPatent researchPatent)
    {
        researchPatent.setUpdatedBy(SecurityUtils.getUsername());
        researchPatent.setUpdatedAt(new Date());

        ResearchPatentTrack patentTrack= new ResearchPatentTrack();
        BeanUtils.copyProperties(researchPatent, patentTrack);
        patentTrack.setActionType("UPDATE");
        researchPatentService.insertResearchPatentAudit(patentTrack);
        return toAjax(researchPatentService.updateResearchPatent(researchPatent));
    }

    /**
     * 删除专利管理
     */
    @RequiresPermissions("outcome:patent:remove")
    @Log(title = "专利管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{patentIds}")
    public AjaxResult remove(@PathVariable Long[] patentIds)
    {
        for (Long patentId : patentIds)
        {
            ResearchPatent researchPatent = researchPatentService.selectResearchPatentByPatentId(patentId);
            researchPatent.setIsDeleted(1);
            researchPatent.setDeletedBy(SecurityUtils.getUsername());
            researchPatent.setDeletedAt(new Date());
            researchPatentService.updateResearchPatent(researchPatent);

            ResearchPatentTrack patentTrack= new ResearchPatentTrack();
            BeanUtils.copyProperties(researchPatent, patentTrack);
            patentTrack.setActionType("DELETE");
            researchPatentService.insertResearchPatentAudit(patentTrack);
        }
        return success();
        //return toAjax(researchPatentService.deleteResearchPatentByPatentIds(patentIds));
    }

    @GetMapping(value = "/patent/{patentId}")
    public AjaxResult getPatentTrack(@PathVariable("patentId") Long patentId)
    {

        return success(researchPatentService.selectResearchPatentAuditList(patentId));
    }
}
